package com.storm.classes.delay
{
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	public class Delay {
		public function Delay(handler:Function, delay:Number) {
			function timerHandler(event:TimerEvent):void {
				if (!_canceled) {
					handler();
				}
				t.stop();
			}
			function completeHandler(event:Event):void {
				t.removeEventListener(Event.DEACTIVATE, completeHandler);
				t.removeEventListener(TimerEvent.TIMER, timerHandler);
			}
			if (delay <= 0) {
				handler();
			} else {
				var t:Timer = new Timer(delay, 1);
				t.addEventListener(TimerEvent.TIMER_COMPLETE, completeHandler);
				t.addEventListener(TimerEvent.TIMER, timerHandler);
				t.start();
			}
		}
		
		private var _canceled:Boolean = false;
		public function cancel():void {
			this._canceled = true;
		}
	}
}